package net.sf.jxls.sample;


import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.sample.model.Department;
import net.sf.jxls.sample.model.Employee;
import net.sf.jxls.transformer.XLSTransformer;

import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * @author Leonid Vysochyn
 */
public class BasicTagSample {
    private static String templateFileName = System.getProperty("user.dir")+"/dhm-datatables-jxls1/templates/basictags.xls";
    private static String destFileName = System.getProperty("user.dir")+"/dhm-datatables-jxls1/out/basictags_output.xls";

    public static void main(String[] args) throws IOException, ParsePropertyException, InvalidFormatException {
        if (args.length >= 2) {
            templateFileName = args[0];
            destFileName = args[1];
        }
        List<Department> departments = new ArrayList<Department>();
        Department department = new Department("IT");
        Employee chief = new Employee("Derek", 35, 3000, 0.30);
        department.setChief(chief);
        department.addEmployee(new Employee("Elsa", 28, 1500, 0.15));
        department.addEmployee(new Employee("Oleg", 32, 2300, 0.25));
        department.addEmployee(new Employee("Neil", 34, 2500, 0.00));
        department.addEmployee(new Employee("Maria", 34, 1700, 0.15));
        department.addEmployee(new Employee("John", 35, 2800, 0.20));
        departments.add(department);
        department = new Department("HR");
        chief = new Employee("Betsy", 37, 2200, 0.30);
        department.setChief(chief);
        department.addEmployee(new Employee("Olga", 26, 1400, 0.20));
        department.addEmployee(new Employee("Helen", 30, 2100, 0.10));
        department.addEmployee(new Employee("Keith", 24, 1800, 0.15));
        department.addEmployee(new Employee("Cat", 34, 1900, 0.15));
        departments.add(department);
        department = new Department("BA");
        chief = new Employee("Wendy", 35, 2900, 0.35);
        department.setChief(chief);
        department.addEmployee(new Employee("Denise", 30, 2400, 0.20));
        department.addEmployee(new Employee("LeAnn", 32, 2200, 0.15));
        department.addEmployee(new Employee("Natali", 28, 2600, 0.10));
        department.addEmployee(new Employee("Martha", 33, 2150, 0.25));
        departments.add(department);
        Map<String,List<Department>> beans = new HashMap<String,List<Department>>();
        beans.put("departments", departments);
        InputStream inputStream = new FileInputStream(templateFileName);
        XLSTransformer transformer = new XLSTransformer();
//        transformer.transformXLS(templateFileName, beans, destFileName);
        Workbook workbook = transformer.transformXLS(inputStream,beans);
        Sheet sheet = workbook.getSheetAt(1);
        int firstRow = 0;
        int lastRow = 0;
        for(Department dept : departments){
            int i = dept.getStaff().size();
            lastRow = firstRow+(i-1);
            sheet.addMergedRegion(new CellRangeAddress(firstRow,(short)lastRow,0,(short)0));
            firstRow = lastRow + 1;
        }

        OutputStream os = new FileOutputStream(destFileName);
        workbook.write(os);
        inputStream.close();
        os.flush();
    }

}
